﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lacrima.Gobang.App
{
    public partial class Gobang : Form
    {
        private Chessboard chessboard { get; set; }

        public Gobang()
        {
            InitializeComponent();
            this.backGroundWorker.DoWork += BackGroundWorker_DoWork;
            this.backGroundWorker.ProgressChanged += BackGroundWorker_ProgressChanged;
            this.backGroundWorker.RunWorkerCompleted += BackGroundWorker_RunWorkerCompleted;

            // 加载棋盘设置
            Graphics g = this.CreateGraphics();
            chessboard = new Chessboard(g);
            //最上面
            this.TopMost = false; ;
        }

        /// <summary>
        /// 新建对局
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNew_Click(object sender, EventArgs e)
        {
            this.backGroundWorker.CancelAsync();
        }

        /// <summary>
        /// 打开棋谱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            // 如果程序正在打开文件则等待其执行完毕
            if(!this.backGroundWorker.IsBusy)
            {
                this.backGroundWorker.RunWorkerAsync();
            }
        }

        /// <summary>
        /// 进度条加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Cancelled)
            {
                this.progressBar.Value = 0;
            }
        }

        /// <summary>
        /// 进度变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackGroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar.Value = e.ProgressPercentage;
        }

        /// <summary>
        /// 执行后台事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = 0;
            while (i < 100)
            {
                Thread.Sleep(100);
                this.backGroundWorker.ReportProgress(++i);

                // 判断取消操作
                if (this.backGroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }

        /// <summary>
        /// 程序退出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuExit_Click(object sender, EventArgs e)
        {
            this.backGroundWorker.DoWork -= BackGroundWorker_DoWork;
            this.backGroundWorker.ProgressChanged -= BackGroundWorker_ProgressChanged;
            this.backGroundWorker.RunWorkerCompleted -= BackGroundWorker_RunWorkerCompleted;
            this.Close();
        }

        private void menuServer_Click(object sender, EventArgs e)
        {
            ServerConfig serverConfig = new ServerConfig();
            serverConfig.ShowDialog();
        }

        private void Gobang_Paint(object sender, PaintEventArgs e)
        {
            //重绘棋盘棋子  
            Thread th = new Thread(new ThreadStart(this.chessboard.Draw));
            th.Start();
        }
    }
}
